home *** CD-ROM | disk | FTP | other *** search
/ PD Collection CD 1 / PD Collection CD 1.iso / textual / pdftops / xpdf / h / Object < prev    next >
Text File  |  1996-06-08  |  9KB  |  327 lines

  1. //========================================================================
  2. //
  3. // Object.h
  4. //
  5. // Copyright 1996 Derek B. Noonburg
  6. //
  7. //========================================================================
  8.  
  9. #ifndef OBJECT_H
  10. #define OBJECT_H
  11.  
  12. #ifdef __GNUC__
  13. //#pragma interface
  14. #endif
  15.  
  16. #include <stdio.h>
  17. #include <string.h>
  18. #include "gtypes.h"
  19. #include "gmem.h"
  20. #include "GString.h"
  21. #include "XRef.h"
  22.  
  23. class Array;
  24. class Dict;
  25. class Stream;
  26.  
  27. //------------------------------------------------------------------------
  28. // Ref
  29. //------------------------------------------------------------------------
  30.  
  31. struct Ref {
  32.   int num;            // object number
  33.   int gen;            // generation number
  34. };
  35.  
  36. //------------------------------------------------------------------------
  37. // object types
  38. //------------------------------------------------------------------------
  39.  
  40. typedef int ObjType;
  41.  
  42. // simple objects
  43. #define objBool     0        // boolean
  44. #define objInt      1        // integer
  45. #define objReal     2        // real
  46. #define objString   3        // string
  47. #define objName     4        // name
  48. #define objNull     5        // null
  49.  
  50. // complex objects
  51. #define objArray    6        // array
  52. #define objDict     7        // dictionary
  53. #define objStream   8        // stream
  54. #define objRef      9        // indirect reference
  55.  
  56. // special objects
  57. #define objCmd      10        // command name
  58. #define objError    11        // error return from Lexer
  59. #define objEOF      12        // end of file return from Lexer
  60. #define objNone     13        // uninitialized object
  61.  
  62. // misc
  63. #define numObjTypes 14        // number of object types
  64.  
  65. //------------------------------------------------------------------------
  66. // Object
  67. //------------------------------------------------------------------------
  68.  
  69. class Object {
  70. public:
  71.  
  72.   // Default constructor.
  73.   Object():
  74.     type(objNone) {}
  75.  
  76.   // Initialize an object.
  77.   Object *initBool(GBool booln1)
  78.     { type = objBool; booln = booln1;
  79. #ifdef DEBUG_MEM
  80.       ++numAlloc[objBool];
  81. #endif
  82.       return this; }
  83.   Object *initInt(int intg1)
  84.     { type = objInt; intg = intg1;
  85. #ifdef DEBUG_MEM
  86.       ++numAlloc[objInt];
  87. #endif
  88.       return this; }
  89.   Object *initReal(double real1)
  90.     { type = objReal; real = real1;
  91. #ifdef DEBUG_MEM
  92.       ++numAlloc[objReal];
  93. #endif
  94.       return this; }
  95.   Object *initString(char *string1, int length)
  96.     { type = objString; string = new GString(string1, length);
  97. #ifdef DEBUG_MEM
  98.       ++numAlloc[objString];
  99. #endif
  100.       return this; }
  101.   Object *initName(char *name1)
  102.     { type = objName; name = copyString(name1);
  103. #ifdef DEBUG_MEM
  104.       ++numAlloc[objName];
  105. #endif
  106.       return this; }
  107.   Object *initNull()
  108.     { type = objNull;
  109. #ifdef DEBUG_MEM
  110.       ++numAlloc[objNull];
  111. #endif
  112.       return this; }
  113.   Object *initArray();
  114.   Object *initDict();
  115.   Object *initStream(Stream *stream1);
  116.   Object *initRef(int num1, int gen1)
  117.     { type = objRef; ref.num = num1; ref.gen = gen1;
  118. #ifdef DEBUG_MEM
  119.       ++numAlloc[objRef];
  120. #endif
  121.       return this; }
  122.   Object *initCmd(char *cmd1)
  123.     { type = objCmd; cmd = copyString(cmd1);
  124. #ifdef DEBUG_MEM
  125.       ++numAlloc[objCmd];
  126. #endif
  127.       return this; }
  128.   Object *initError()
  129.     { type = objError;
  130. #ifdef DEBUG_MEM
  131.       ++numAlloc[objError];
  132. #endif
  133.       return this; }
  134.   Object *initEOF()
  135.     { type = objEOF;
  136. #ifdef DEBUG_MEM
  137.       ++numAlloc[objEOF];
  138. #endif
  139.       return this; }
  140.  
  141.   // Copy an object.
  142.   Object *copy(Object *obj);
  143.  
  144.   // If object is a Ref, fetch and return the referenced object.
  145.   // Otherwise, return a copy of the object.
  146.   Object *fetch(Object *obj)
  147.     { return (type == objRef && xref) ?
  148.          xref->fetch(ref.num, ref.gen, obj) : copy(obj); }
  149.  
  150.   // Free object contents.
  151.   void free();
  152.  
  153.   // Type checking.
  154.   GBool isBool() { return type == objBool; }
  155.   GBool isInt() { return type == objInt; }
  156.   GBool isReal() { return type == objReal; }
  157.   GBool isNum() { return type == objInt || type == objReal; }
  158.   GBool isString() { return type == objString; }
  159.   GBool isName() { return type == objName; }
  160.   GBool isNull() { return type == objNull; }
  161.   GBool isArray() { return type == objArray; }
  162.   GBool isDict() { return type == objDict; }
  163.   GBool isStream() { return type == objStream; }
  164.   GBool isRef() { return type == objRef; }
  165.   GBool isCmd() { return type == objCmd; }
  166.   GBool isError() { return type == objError; }
  167.   GBool isEOF() { return type == objEOF; }
  168.   GBool isNone() { return type == objNone; }
  169.  
  170.   // Special type checking.
  171.   GBool isName(char *name1)
  172.     { return type == objName && !strcmp(name, name1); }
  173.   GBool isDict(char *dictType);
  174.   GBool isStream(char *dictType);
  175.   GBool isCmd(char *cmd1)
  176.     { return type == objCmd && !strcmp(cmd, cmd1); }
  177.  
  178.   // Accessors.  NB: these assume object is of correct type.
  179.   GBool getBool() { return booln; }
  180.   int getInt() { return intg; }
  181.   double getReal() { return real; }
  182.   double getNum() { return type == objInt ? (double)intg : real; }
  183.   GString *getString() { return string; }
  184.   char *getName() { return name; }
  185.   Array *getArray() { return array; }
  186.   Dict *getDict() { return dict; }
  187.   Stream *getStream() { return stream; }
  188.   int getRefNum() { return ref.num; }
  189.   int getRefGen() { return ref.gen; }
  190.  
  191.   // Array accessors.
  192.   int arrayGetLength();
  193.   void arrayAdd(Object *elem);
  194.   Object *arrayGet(int i, Object *obj);
  195.   Object *arrayGetNF(int i, Object *obj);
  196.  
  197.   // Dict accessors.
  198.   int dictGetLength();
  199.   void dictAdd(char *key, Object *val);
  200.   GBool dictIs(char *dictType);
  201.   Object *dictLookup(char *key, Object *obj);
  202.   Object *dictLookupNF(char *key, Object *obj);
  203.   char *dictGetKey(int i);
  204.   Object *dictGetVal(int i, Object *obj);
  205.   Object *dictGetValNF(int i, Object *obj);
  206.  
  207.   // Stream accessors.
  208.   GBool streamIs(char *dictType);
  209.   void streamReset();
  210.   int streamGetChar();
  211.   int streamGetPos();
  212.   void streamSetPos(int pos);
  213.   FILE *streamGetFile();
  214.   Dict *streamGetDict();
  215.  
  216.   // Output.
  217.   char *getTypeName();
  218.   void print(FILE *f = stdout);
  219.  
  220.   // Memory testing.
  221.   static void memCheck(FILE *f);
  222.  
  223. private:
  224.  
  225.   ObjType type;            // object type
  226.   union {            // value for each type:
  227.     GBool booln;        //   boolean
  228.     int intg;            //   integer
  229.     double real;        //   real
  230.     GString *string;        //   string
  231.     char *name;            //   name
  232.     Array *array;        //   array
  233.     Dict *dict;            //   dictionary
  234.     Stream *stream;        //   stream
  235.     Ref ref;            //   indirect reference
  236.     char *cmd;            //   command
  237.   };
  238.  
  239. #ifdef DEBUG_MEM
  240.   static int            // number of each type of object
  241.     numAlloc[numObjTypes];    //   currently allocated
  242. #endif
  243. };
  244.  
  245. //------------------------------------------------------------------------
  246. // Array accessors.
  247. //------------------------------------------------------------------------
  248.  
  249. #include "Array.h"
  250.  
  251. inline int Object::arrayGetLength()
  252.   { return array->getLength(); }
  253.  
  254. inline void Object::arrayAdd(Object *elem)
  255.   { array->add(elem); }
  256.  
  257. inline Object *Object::arrayGet(int i, Object *obj)
  258.   { return array->get(i, obj); }
  259.  
  260. inline Object *Object::arrayGetNF(int i, Object *obj)
  261.   { return array->getNF(i, obj); }
  262.  
  263. //------------------------------------------------------------------------
  264. // Dict accessors.
  265. //------------------------------------------------------------------------
  266.  
  267. #include "Dict.h"
  268.  
  269. inline int Object::dictGetLength()
  270.   { return dict->getLength(); }
  271.  
  272. inline void Object::dictAdd(char *key, Object *val)
  273.   { dict->add(key, val); }
  274.  
  275. inline GBool Object::dictIs(char *dictType)
  276.   { return dict->is(dictType); }
  277.  
  278. inline GBool Object::isDict(char *dictType)
  279.   { return type == objDict && dictIs(dictType); }
  280.  
  281. inline Object *Object::dictLookup(char *key, Object *obj)
  282.   { return dict->lookup(key, obj); }
  283.  
  284. inline Object *Object::dictLookupNF(char *key, Object *obj)
  285.   { return dict->lookupNF(key, obj); }
  286.  
  287. inline char *Object::dictGetKey(int i)
  288.   { return dict->getKey(i); }
  289.  
  290. inline Object *Object::dictGetVal(int i, Object *obj)
  291.   { return dict->getVal(i, obj); }
  292.  
  293. inline Object *Object::dictGetValNF(int i, Object *obj)
  294.   { return dict->getValNF(i, obj); }
  295.  
  296. //------------------------------------------------------------------------
  297. // Stream accessors.
  298. //------------------------------------------------------------------------
  299.  
  300. #include "Stream.h"
  301.  
  302. inline GBool Object::streamIs(char *dictType)
  303.   { return stream->getDict()->is(dictType); }
  304.  
  305. inline GBool Object::isStream(char *dictType)
  306.   { return type == objStream && streamIs(dictType); }
  307.  
  308. inline void Object::streamReset()
  309.   { stream->reset(); }
  310.  
  311. inline int Object::streamGetChar()
  312.   { return stream->getChar(); }
  313.  
  314. inline int Object::streamGetPos()
  315.   { return stream->getPos(); }
  316.  
  317. inline void Object::streamSetPos(int pos)
  318.   { stream->setPos(pos); }
  319.  
  320. inline FILE *Object::streamGetFile()
  321.   { return stream->getFile(); }
  322.  
  323. inline Dict *Object::streamGetDict()
  324.   { return stream->getDict(); }
  325.  
  326. #endif
  327.